// component/pending/index.ts
import { searchMaintainer, stewardOrder } from '../../api/order'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Array
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isFresh: false as boolean,
    show: false as boolean,
    username: '' as string,
    beforeClose: null as null,
    comID: -1 as number,
    userID: -1 as number,
    isPicker: false as boolean,
    columns: [] as string[],
    repairReasonId: -1 as number,
    repairReasonName: '' as string,
    orderId: -1 as number,
    identity: '' as string
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleDetail(e: any) {
      const { id } = e.currentTarget.dataset
      wx.navigateTo({
        url: '/subpackagesA/pages/repairdetails/repairdetails?id=' + id
      })
    },
    handleDialog(e: any) {
      const comID = this.data.comID;
      const userID = this.data.userID;
      const { id } = e.currentTarget.dataset
      this.setData({
        show: true,
        orderId: id
      })
      searchMaintainer(comID, userID).then((res: any) => {
        this.setData({
          columns: res.data
        })
        console.log(res)
      })
    },
    handleInput() {
      this.setData({
        isPicker: true
      })
    },
    handleSubmit() {
      const username = this.data.username
      this.setData({
        beforeClose: (action) => {
          new Promise((resolve) => {
            if (action === 'confirm') {
              console.log(username)
              if (username == '') {
                wx.showToast({
                  icon: 'error',
                  title: '维修工未选'
                })
                resolve(false)
              }
            } else {
              resolve(true)
            }
          })
        }
      })
      if (username != '') {
        const comID = this.data.comID;
        const userID = this.data.userID;
        const repairPersonID = this.data.repairReasonId;
        const orderID = this.data.orderId
        const repairReasonName = this.data.repairReasonName
        stewardOrder(comID, userID, repairPersonID, orderID, repairReasonName).then((res: any) => {
          if (res.data.code == 200) {
            wx.showToast({
              title: '派单成功'
            })
          } else {
            wx.showToast({
              icon: 'error',
              title: '派单失败'
            })
          }
          console.log(res)
        })
        this.setData({
          show: false
        })
      }
    },
    handldeClose() {
      this.setData({
        show: false
      })
    },
    onPickerClose() {
      this.setData({
        isPicker: false
      })
    },
    onPicker(e:any) {
      console.log(e)
      this.setData({
        username: e.detail.value.repairReason,
        repairReasonId: e.detail.value.id,
        repairReasonName: e.detail.value.repairReason,
        isPicker: false
      })
    }
  },
  //小程序声明周期
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID,
        identity: wx.getStorageSync("userInfo").data.position
      });

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        userID: wx.getStorageSync("userInfo").data.userID
      });
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function () {
      // 页面尺寸变化
    }
  }
})